package o;

import android.content.Context;
import android.os.Bundle;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.cloudservice.CloudAccount;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.gamebox.plugin.gameservice.bean.GameInfo;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant.KeyAndValue;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ezl {
    private static ezl ˊ = null;
    private List<ezm> ˎ = new ArrayList();
    private Context ˏ = null;
    private d ॱ = new d();

    class d extends eow {
        final /* synthetic */ ezl ˊ;

        private d(ezl o_ezl) {
            this.ˊ = o_ezl;
        }

        public void onError(ErrorStatus errorStatus) {
            int errorCode = errorStatus.getErrorCode();
            String errorReason = errorStatus.getErrorReason();
            Logger.e("GameAccountManagerHelper", "AccountLoginHandler onError, errCode=" + errorCode + ", reason=" + errorReason);
            super.onError(errorStatus);
            ArrayList arrayList = new ArrayList(this.ˊ.ˎ);
            this.ˊ.ˎ.clear();
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                ezm o_ezm = (ezm) it.next();
                if (errorCode == 3002) {
                    o_ezm.ˏ(3, errorReason);
                } else if (o_ezm.ˋ()) {
                    o_ezm.ˏ(4, errorReason);
                } else {
                    o_ezm.ˏ(errorCode, errorReason);
                }
                it.remove();
            }
        }

        public void onFinish(CloudAccount[] cloudAccountArr) {
            this.ˊ.ˊ();
        }

        protected String ˊ() {
            return "getAccountsByType";
        }

        public void onLogout(CloudAccount[] cloudAccountArr, int i) {
            if (Logger.ˎ()) {
                Logger.ˏ("GameAccountManagerHelper", "onLogout " + i);
            }
            if (!(i == -1 || epz.ॱ(cloudAccountArr))) {
                for (CloudAccount cloudAccount : cloudAccountArr) {
                    if (cloudAccount != null) {
                        cloudAccount.logout(this.ˊ.ˏ);
                    }
                }
            }
            this.ˊ.ˊ();
        }

        public void onLogin(CloudAccount[] cloudAccountArr, int i) {
            ArrayList arrayList = new ArrayList(this.ˊ.ˎ);
            this.ˊ.ˎ.clear();
            ccm ॱ = this.ˊ.ॱ(cloudAccountArr, i);
            if (ॱ == null) {
                if (Logger.ˎ()) {
                    Logger.ˏ("GameAccountManagerHelper", "onLogin, accountInfo is null");
                }
                Iterator it = arrayList.iterator();
                while (it.hasNext()) {
                    ((ezm) it.next()).ˏ(1, "");
                }
            } else if (arrayList.size() != 0) {
                Iterator it2 = arrayList.iterator();
                while (it2.hasNext()) {
                    ((ezm) it2.next()).ˊ(ॱ);
                    it2.remove();
                }
            } else if (Logger.ˎ()) {
                Logger.ˏ("GameAccountManagerHelper", "loginCallBacks.size() == 0");
            }
        }
    }

    private ezl() {
    }

    public static ezl ˎ() {
        if (ˊ == null) {
            ˊ = new ezl();
        }
        return ˊ;
    }

    boolean ˏ(Context context) {
        return eod.ˋ(context);
    }

    public void ˋ(Context context, boolean z, boolean z2, ezm o_ezm) {
        if (Logger.ˎ()) {
            Logger.ˏ("GameAccountManagerHelper", "gameLogin needAuth:" + z + ", isSTAuth:" + z2);
        }
        if (o_ezm != null) {
            o_ezm.ˋ(true);
            o_ezm.ˏ(z2);
            o_ezm.ˊ(z);
            try {
                long loginCount = CloudAccountManager.getLoginCount(context);
                if (Logger.ˎ()) {
                    Logger.ˏ("GameAccountManagerHelper", "loginCount:" + loginCount);
                }
                if (o_ezm.ˋ() && loginCount == 0) {
                    if (Logger.ˎ()) {
                        Logger.ˏ("GameAccountManagerHelper", "start quickLogin.");
                    }
                    this.ˎ.add(o_ezm);
                    CloudAccountManager.quickLogin(context, this.ॱ);
                    return;
                }
                ˏ(context, o_ezm);
            } catch (Exception e) {
                Logger.ˋ("GameAccountManagerHelper", "gameLogin fail:" + e.toString());
                ˏ(context, o_ezm);
            }
        }
    }

    public void ˏ() {
        fab.ˎ().ˊ(null, "login|buoy", null, false);
        fae ˏ = fam.ॱ().ˏ();
        if (ˏ != null) {
            GameInfo gameInfo = ˏ.getGameInfo();
            if (gameInfo != null) {
                dgh.ॱ(ContentManager.getInstant().getContext(), "15150506", new StringBuffer().append(KeyAndValue.NUMBER_01).append(HiAnalyticsConstant.REPORT_VAL_SEPARATOR).append(UserSession.getInstance().getUserId()).append(HiAnalyticsConstant.REPORT_VAL_SEPARATOR).append(gameInfo.getPackageName()).append(HiAnalyticsConstant.REPORT_VAL_SEPARATOR).append(gameInfo.getAppId()).append(HiAnalyticsConstant.REPORT_VAL_SEPARATOR).append(gameInfo.getSdkVersionCode()).toString());
            }
        }
    }

    private void ˏ(Context context, ezm o_ezm) {
        if (Logger.ˎ()) {
            Logger.ˏ("GameAccountManagerHelper", "enter loginDirectly: " + o_ezm);
        }
        this.ˏ = context;
        boolean z = o_ezm != null && o_ezm.ˊ();
        if (!ˏ(context, z)) {
            if (o_ezm != null) {
                this.ˎ.add(o_ezm);
            }
            CloudAccountManager.getAccountsByType(context, ContentManager.getInstant().getContext().getPackageName(), ˊ(o_ezm), this.ॱ);
        } else if (Logger.ˎ()) {
            Logger.ˏ("GameAccountManagerHelper", "login no network");
        }
    }

    private Bundle ˊ(ezm o_ezm) {
        boolean z = false;
        Bundle bundle = new Bundle();
        bundle.putInt(CloudAccountManager.KEY_LOGIN_CHANNEL, 19000001);
        bundle.putInt("scope", 1);
        String str = CloudAccountManager.KEY_NEEDAUTH;
        boolean z2 = o_ezm == null || o_ezm.ˏ();
        bundle.putBoolean(str, z2);
        bundle.putBoolean(CloudAccountManager.KEY_IS_TRANS_NAVIGATION_BAR, true);
        if (o_ezm != null && o_ezm.ॱ()) {
            z = true;
        }
        if (Logger.ˎ()) {
            Logger.ˏ("GameAccountManagerHelper", "initBundle useAIDL:" + z);
        }
        if (z) {
            bundle.putBoolean(CloudAccountManager.KEY_AIDL, true);
        }
        return bundle;
    }

    private ccm ॱ(CloudAccount[] cloudAccountArr, int i) {
        if (cloudAccountArr == null || i < 0 || cloudAccountArr.length <= i) {
            Logger.e("GameAccountManagerHelper", "getCorrectAccount, mAccounts is null or index is invalid.");
            return null;
        }
        if (Logger.ˎ()) {
            Logger.ˏ("GameAccountManagerHelper", "getCorrectAccount, mAccounts.length = " + cloudAccountArr.length + ", index = " + i);
        }
        CloudAccount cloudAccount = cloudAccountArr[i];
        if (cloudAccount != null) {
            Bundle accountInfo = cloudAccount.getAccountInfo();
            if (accountInfo == null) {
                Logger.e("GameAccountManagerHelper", "getCorrectAccount, bundle of getAccountInfo() is null.");
                return null;
            }
            String string = accountInfo.getString("userId");
            String authToken = cloudAccount.getAuthToken();
            if (dof.ˋ(string) || dof.ˋ(authToken)) {
                Logger.e("GameAccountManagerHelper", "getCorrectAccount, mAccounts is invalid because userId or serviceToken is null.");
                return null;
            } else if ("1".equals(accountInfo.getString("STValidStatus"))) {
                Logger.e("GameAccountManagerHelper", "getCorrectAccount, mAccounts is invalid because serviceToken is invalid.");
                return null;
            } else {
                ccm o_ccm = new ccm();
                o_ccm.ˋ(string);
                o_ccm.ˎ(authToken);
                o_ccm.ˊ(accountInfo.getString("loginUserName"));
                o_ccm.ॱ(accountInfo.getString("accountName"));
                o_ccm.ˏ(accountInfo.getString("deviceType"));
                o_ccm.ॱॱ(accountInfo.getString("deviceId"));
                return o_ccm;
            }
        }
        Logger.e("GameAccountManagerHelper", "getCorrectAccount, mAccounts[index] is null.");
        return null;
    }

    private boolean ˏ(Context context, boolean z) {
        if (doo.ˊ(context)) {
            return false;
        }
        if (z) {
            evk.ˋ(context, context.getResources().getString(exp$f.ʽˋ), 0).ˋ();
        }
        return true;
    }

    private void ˊ() {
        if (Logger.ˎ()) {
            Logger.ˏ("GameAccountManagerHelper", "clear loginCallBacks");
        }
        this.ˎ.clear();
    }
}
